#_*_coding:utf-8_*_
from django.shortcuts import render,HttpResponseRedirect,HttpResponse,redirect
from django.http import HttpResponseNotFound,Http404
from django.contrib import auth

from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
from web import host_mgr
# Create your views here.
from web import models,utils
import json,datetime,os,time
from CrazyEye import settings
from  web import forms
from backend.utils import json_date_to_stamp,json_date_handler
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
import django.utils.timezone
from django.core.exceptions import ObjectDoesNotExist
from web import tables
from web import admin
from web.king_admin import enabled_admins
from backend import audit as session_audit
from web import permissions
import random,string
import subprocess
import requests
import hashlib
from backend import aliyun
#from web.utils import login_required


@permissions.check_permission
@login_required
def dashboard(request):
    if request.user.is_superuser:
        recent_tasks= models.TaskLog.objects.all().order_by('-id')[:10]
        return render(request,'index.html',{
            'login_user':request.user,
            'recent_tasks':recent_tasks
        })
    else:
        return  HttpResponseRedirect('/hosts/')


@login_required
def hosts(request):

    selected_g_id = request.GET.get('selected_group')
    if selected_g_id:
        if selected_g_id.isdigit():
            selected_g_id = int(selected_g_id)

    recent_logins = utils.recent_accssed_hosts(request)

    return render(request,'hosts.html',{'login_user':request.user,
                                         'selected_g_id': selected_g_id,
                                        'active_node':"/hosts/?selected_group=-1",
                                        'recent_logins':recent_logins,
                                        'webssh':settings.SHELLINABOX})


@login_required
def hostsinfo(request):
    if request.method=='GET':
        return render(request,'host_info.html',{'active_node':'/hosts/info/'})
    else:
        ip=request.POST.get('key',None)
        page=int(request.POST.get('page',None))
        if ip:
            hinfo_list=models.HostInfo.objects.filter(host__ip_addr=ip)
        else:
            try:
                page=page
            except Exception as e:
                page=1
            hinfo_list = models.HostInfo.objects.all()
        page_num = int(len(hinfo_list) / 10) + 1
        hinfo_list=[[h.host.hostname,h.host.ip_addr,h.host.account.username,h.host.idc.name,h.mem,h.cpu,h.bandwidth] for h in hinfo_list][(page-1)*10:page*10]
        result={'hinfo_list':hinfo_list,'page_num':page_num}

        result=json.dumps(result)
        return HttpResponse(result)


@login_required
def cdndomain_info(request):
    if request.method=='GET':
        return render(request,'cdndomain_info.html',{'active_node':'/cdndomain/info/'})
    else:
        domain=request.POST.get('key',None)
        page=int(request.POST.get('page',None))
        if domain:
            cdndomain_list=models.CDNDomain.objects.filter(domain_name=domain)
        else:
            try:
                page=page
            except Exception as e:
                page=1
            cdndomain_list = models.CDNDomain.objects.all()
        page_num = int(len(cdndomain_list) / 10) + 1
        cdndomain_list=[[h.domain_name,h.cname,h.source,h.ishttps,h.account.username] for h in cdndomain_list][(page-1)*10:page*10]
        result={'cdndomain_list':cdndomain_list,'page_num':page_num}

        result=json.dumps(result)
        return HttpResponse(result)

@login_required
def cdndata_info(request):
    if request.method=='GET':
        account_list=models.Account.objects.filter(access_id__isnull=False,product='aliyun').exclude(access_id__exact='')
        return render(request,'cdndata_history.html',{'active_node':'/cdndata/info/','account_list':account_list})
    else:
        account=request.POST.get('key',None)
        page=int(request.POST.get('page',None))
        if account:
            cdndata_list=models.CDNData.objects.filter(account__username=account).order_by('-timestamps')[:30]
        else:
            try:
                page=page
            except Exception as e:
                page=1
            cdndata_list = models.CDNData.objects.all()
        page_num = int(len(cdndata_list) / 10) + 1
        cdndata_list=[[h.date_time,h.account.username,h.maxbps,h.totaltraffic] for h in cdndata_list][(page-1)*10:page*10]
        result={'cdndata_list':cdndata_list,'page_num':page_num}

        result=json.dumps(result)
        return HttpResponse(result)

def login(request):

    if request.method == "POST":

        username = request.POST.get('username')
        password = request.POST.get('password')
        user = auth.authenticate(username=username,password=password)
        if user is not None:
            try:
                if user.valid_begin_time and user.valid_end_time:
                    if django.utils.timezone.now() > user.valid_begin_time and django.utils.timezone.now()  < user.valid_end_time:
                        auth.login(request,user)
                        request.session.set_expiry(60*60*5)
                        return HttpResponseRedirect(request.GET.get("next") if request.GET.get("next") else "/")
                    else:
                        return render(request,'login.html',{'login_err': 'User account is expired,please contact your IT guy for this!'})
                else:
                    auth.login(request, user)
                    request.session.set_expiry(60*60*5)
                    return HttpResponseRedirect(request.GET.get("next") if request.GET.get("next") else "/")

            except ObjectDoesNotExist:
                    return render(request,'login.html',{'login_err': u'CrazyEye账户还未设定,请先登录后台管理界面创建CrazyEye账户!'})

        else:
            return render(request,'login.html',{'login_err': 'Wrong username or password!'})
    else:
        return render(request, 'login.html')

@login_required
def personal(request):
    if request.method == 'POST':
        msg = {}
        old_passwd = request.POST.get('old_passwd')

        new_password = request.POST.get('new_passwd')
        user = auth.authenticate(username=request.user.email,password=old_passwd)
        if user is not None:
            request.user.set_password(new_password)
            request.user.save()
            msg['msg'] = 'Password has been changed!'
            msg['res'] = 'success'
        else:
            msg['msg'] = 'Old password is incorrect!'
            msg['res'] = 'failed'

        return HttpResponse(json.dumps(msg))
    else:
        print(request.user.name)
        return render(request,'personal.html',{'info_form':forms.UserProfileForm()})

@login_required
def logout(request):
    auth.logout(request)
    return HttpResponseRedirect("/")

@login_required
def version_change(request):
    if request.method=='POST':
        IP=request.POST.get('IP')
        version=request.POST.get('version')
        cmd='sh /root/version_change_v%s.sh;echo $?' % (str(version))
        result=utils.ssh_cmd(IP,22,cmd)
        return HttpResponse(result)
    else:
        return render(request,'version_change.html')

@login_required
def download_switch(request):
    if request.method=='POST':
        id=request.POST.get('id')
        new_download=request.POST.get('new_download')
        message=utils.download_switch(id,new_download)
        message=json.dumps(message)
        return HttpResponse(message)
    else:
        download_list=models.DownloadUrl.objects.filter(enabled=True).values('id','name').order_by('-id_name')
        return render(request,'download_switch.html',{'download_list':download_list,'active_node':'/download_switch/'})

@login_required
def ed2k_switch(request):
    if request.method=="POST":
        id_name=request.POST.get('id_name')
        new_download=request.POST.get('new_download')
        action=request.POST.get('action')
        message=utils.ed2k_switch(id_name,new_download,action)
        message=json.dumps(message)
        return HttpResponse(message)
    else:
        download_list=models.DownloadUrl.objects.filter(enabled=True).values('id_name','name').order_by('-id_name')
        return render(request,'ed2k_switch.html',{'download_list':download_list,'active_node':'/ed2k_switch/'})

@login_required
def password_reset_form(request,table_db_name,user_id):
    user_obj = models.UserProfile.objects.get(id=user_id)
    if request.method == "GET":
        change_form = enabled_admins[table_db_name].add_form(instance=user_obj)
    else:
        change_form = enabled_admins[table_db_name].add_form(request.POST,instance=user_obj)
        if change_form.is_valid():
            change_form.save()
            url = "/%s/" %request.path.strip("/password/")
            return redirect(url)

    return render(request,'password_change.html',{'user_obj':user_obj,
                                                  'form':change_form})


@permissions.check_permission
@login_required
def hosts_multi(request):
    #valid_hosts = host_mgr.valid_host_list(request) #dict
    recent_tasks = models.TaskLog.objects.filter(user_id=1).order_by('-id')[:10]


    return render(request,'hosts_multi.html',{'login_user':request.user,
                                              #'host_groups':valid_hosts,
                                              'recent_tasks': recent_tasks,
                                              'active_node':'/hosts/multi'})


@login_required
def hosts_add(request):
    random_str = ''.join(random.sample(string.ascii_lowercase, 8))
    return render(request,'hosts_add.html',{'login_user':request.user,
                                              'random_str': random_str,
                                              'active_node':'/hosts/add/'}
                  )

@login_required
def hosts_upload(request):
    dir='/tmp'
    #files=request.FILES.items()
    print(request.FILES.items())
    for k,f in request.FILES.items():
        with open(dir+"/"+f.name, 'wb+') as destination:
            for chunk in f.chunks():
                destination.write(chunk)
        message=utils.hosts_insert(dir+"/"+f.name)

    return HttpResponse(message)

@login_required
def multitask_cmd(request):
    #print '==post:',request.POST
    multi_task = host_mgr.MultiTask('run_cmd',request)
    task_id = multi_task.run()
    if task_id:
        return HttpResponse(task_id)
    else:
        return HttpResponse("TaskCreatingError")

@login_required
def crontab(request):
    return  render(request,'crontab.html',{'active_node':'/hosts/crontab/'})


@permissions.check_permission
@login_required
def hosts_multi_filetrans(request):

    random_str = ''.join(random.sample(string.ascii_lowercase,8))
    recent_tasks = models.TaskLog.objects.filter(user_id=1).order_by('-id')[:10]


    return render(request,'hosts_multi_files.html',{'login_user':request.user,
                                              'recent_tasks': recent_tasks,
                                              'random_str': random_str,
                                              'active_node':'/hosts/multi/filetrans'})



def get_uploaded_fileinfo(file_dic,upload_dir):
    for filename in os.listdir(upload_dir):
        abs_file = '%s/%s' % (upload_dir, filename)
        file_create_time = time.strftime("%Y-%m-%d %H:%M:%S",
                                         time.gmtime(os.path.getctime(abs_file)))
        file_dic['files'][filename] = {'size': os.path.getsize(abs_file) / 1000,
                                           'ctime': file_create_time}


@login_required
@csrf_exempt
def multitask_file_upload(request,random_str):
    print('---random str:',random_str,request.FILES)
    upload_dir = "%s/task_data/tmp/%s" %(settings.FileUploadDir,random_str)
    response_dic = {'files':{}}
    utils.handle_upload_file(request,random_str,response_dic)
    get_uploaded_fileinfo(response_dic, upload_dir)

    return HttpResponse(json.dumps(response_dic))
@login_required
def multitask_file(request):
    multi_task = host_mgr.MultiTask(request.POST.get('task_type'),request)
    task_result = multi_task.run()
    return  HttpResponse(task_result)




@login_required
def delete_file(request,random_str):
    response = {}
    if request.method == "POST":
        upload_dir = "%s/task_data/tmp/%s" % (settings.FileUploadDir,random_str)
        filename = request.POST.get('filename')
        file_abs = "%s/%s" %(upload_dir,filename.strip())
        if os.path.isfile(file_abs):
            os.remove(file_abs)
            response['msg'] = "file '%s' got deleted " % filename
        else:
            response["error"] = "file '%s' does not exist on server"% filename
    else:
        response['error'] = "only supoort POST method..."
    return HttpResponse(json.dumps(response))


@login_required
def multitask_res(request):
    multi_task = host_mgr.MultiTask('get_task_result',request)
    task_result = multi_task.run()
    return HttpResponse(task_result)

@login_required
def file_download(request,task_id):

    file_path = "%s/task_data/%s" %(settings.FileUploadDir,task_id)
    return utils.send_zipfile(request, task_id,file_path)


@login_required
def token_gen(request):
    #token_type = request.POST.get('token_type')
    token = utils.Token(request)
    token_key = token.generate()

    return HttpResponse(token_key)

@login_required
def dashboard_summary(request):

    if request.method == 'GET':


        summary_data = utils.dashboard_summary(request)
        return HttpResponse(json.dumps(summary_data,default=json_date_to_stamp))

@login_required
def dashboard_detail(request):
    if request.method == 'GET':
        detail_ins = utils.Dashboard(request)
        res = list(detail_ins.get())
        return HttpResponse(json.dumps(res,default=json_date_handler))

@login_required
def host_detail(request):
    host_id = request.GET.get('host_id')
    access_records = []

    all_hosts = models.Hosts.objects.all()
    if host_id:
        host_id = int(host_id)

        #access_records = models.AuditLog.objects.filter(host__host_id=host_id,action_type=1).order_by('-date')
        access_records = models.Session.objects.filter(bind_host__host_id=host_id).order_by('-date')
        #print("acc records;",access_records)

        paginator = Paginator(access_records,10)
        page = request.GET.get('page')
        try:
            access_records = paginator.page(page)
        except PageNotAnInteger:
            access_records = paginator.page(1)
        except EmptyPage:
            access_records = paginator.page(paginator.num_pages)


    return  render(request, 'host_detail.html', {'all_hosts':all_hosts,
                                                 'current_host_id': host_id,
                                                 'access_records': access_records,
                                                 'active_node':'/host/detail/'})



@login_required
def domain_info(request):
    if request.method=='GET':
        domain_id = request.GET.get('domain_id')

        all_domains = models.Domain.objects.all()
        if domain_id:
            domain_id = int(domain_id)

        return  render(request, 'domain_search.html', {'all_domains':all_domains,
                                                     'current_domain_id': domain_id,
                                                     'active_node':'/domain/'})
    else:
        domain_id=request.POST.get('domain_id')
        domainobj=models.Domain.objects.get(id=int(domain_id))
        result={}
        result['domain']=domainobj.name
        result['hostname']=domainobj.host.hostname
        result['ip']=domainobj.host.ip_addr
        result['idc']=domainobj.host.idc.name
        result['account']=domainobj.host.account.username
        result['conf']=domainobj.conf
        result=json.dumps(result)
        return HttpResponse(result)


@login_required
def site_detail(request):
    host_id = request.GET.get('host_id')
    nginx_conf = []

    all_hosts = models.Hosts.objects.all()
    if host_id:
        host_id = int(host_id)
        hostobj=models.Hosts.objects.get(id=host_id)
        cmd='ls -l /usr/local/webserver/nginx/conf/vhosts/*.conf'
        result=utils.ssh_cmd(hostobj.ip_addr,hostobj.port,cmd)
        for line in result:
            line=line.split()
            nginx_conf.append(line[-1].strip("\n"))
        print(nginx_conf)
        paginator = Paginator(nginx_conf,10)
        page = request.GET.get('page')
        try:
            nginx_conf = paginator.page(page)
        except PageNotAnInteger:
            nginx_conf = paginator.page(1)
        except EmptyPage:
            nginx_conf = paginator.page(paginator.num_pages)


    return  render(request, 'nginx_conf.html', {'all_hosts':all_hosts,
                                                 'current_host_id': host_id,
                                                 'nginx_conf': nginx_conf,
                                                 'active_node':'/host/site/'})

@login_required

def nginx_edit(request):
    BACKUP_DIR='/tmp'
    if request.method=="GET":
        host_id=request.GET.get('host_id')
        conf_path=request.GET.get('conf_path')
        hostobj = models.Hosts.objects.get(id=host_id)
        cmd='/bin/cp -f %s %s;cat %s' % (conf_path,os.path.join(BACKUP_DIR,os.path.basename(conf_path)),conf_path)
        result=utils.ssh_cmd(hostobj.ip_addr,hostobj.port,cmd)
        data={}
        data['conf']=result
        data['ip']=hostobj.ip_addr
        data['hostname']=hostobj.hostname
        data['conf']=''.join(data['conf'])
        print(data)
        data=json.dumps(data)
        return HttpResponse(data)
    elif request.method=="POST":
        conf=request.POST.get('conf')
        conf=conf.replace('\r','')
        host_id=request.POST.get('host_id')
        conf_path=request.POST.get('conf_path')
        hostobj = models.Hosts.objects.get(id=host_id)
        cmd="echo '%s' > %s;/usr/local/webserver/nginx/sbin/nginx -t >/dev/null 2>&1 ;echo $?" % (conf,conf_path)
        result=utils.ssh_cmd(hostobj.ip_addr,hostobj.port,cmd)
        if result==0:
            cmd="/usr/local/webserver/nginx/sbin/nginx -s reload"
            utils.ssh_cmd(hostobj.ip_addr, hostobj.port, cmd)
        return HttpResponse(result)


@login_required
def nginx_cancel(request):
    if request.method=="POST":
        host_id = request.POST.get('host_id')
        conf_path = request.POST.get('conf_path')
        hostobj = models.Hosts.objects.get(id=host_id)
        cmd="/bin/cp -f /tmp/%s %s;/usr/local/webserver/nginx/sbin/nginx -t >/dev/null 2>&1 ;echo $?" % (os.path.basename(conf_path), conf_path)
        result = utils.ssh_cmd(hostobj.ip_addr,hostobj.port,cmd)
        return HttpResponse(result)


@login_required
def nginx_log(request):
    host_id = request.GET.get('host_id')
    nginx_log = []

    all_hosts = models.Hosts.objects.all()
    if host_id:
        host_id = int(host_id)
        hostobj = models.Hosts.objects.get(id=host_id)
        cmd="find /data0/logs/nginx -type f -exec ls -lh {} \;"
        result = utils.ssh_cmd(hostobj.ip_addr,hostobj.port,cmd)
        for line in result:
            line = line.split()
            nginx_log.append([line[-1].strip("\n"),line[4]])
        paginator = Paginator(nginx_log, 10)
        page = request.GET.get('page')
        try:
            nginx_log = paginator.page(page)
        except PageNotAnInteger:
            nginx_log = paginator.page(1)
        except EmptyPage:
            nginx_log = paginator.page(paginator.num_pages)

    return render(request, 'nginx_log.html', {'all_hosts': all_hosts,
                                               'current_host_id': host_id,
                                               'nginx_log': nginx_log,
                                               'active_node': '/host/nginx_log/'})

@login_required
def mysql_log(request):
    host_id = request.GET.get('host_id')
    mysql_log = []

    all_hosts = models.Hosts.objects.all()
    if host_id:
        host_id = int(host_id)
        hostobj = models.Hosts.objects.get(id=host_id)
        cmd="find /data0/mysql/3306/logs -type f -maxdepth 1 -exec ls -lh {} \;"
        result = utils.ssh_cmd(hostobj.ip_addr,hostobj.port,cmd)
        for line in result:
            line = line.split()
            mysql_log.append([line[-1].strip("\n"),line[4]])
        paginator = Paginator(mysql_log, 10)
        page = request.GET.get('page')
        try:
            mysql_log = paginator.page(page)
        except PageNotAnInteger:
            mysql_log = paginator.page(1)
        except EmptyPage:
            mysql_log = paginator.page(paginator.num_pages)

    return render(request, 'mysql_log.html', {'all_hosts': all_hosts,
                                               'current_host_id': host_id,
                                               'mysql_log': mysql_log,
                                               'active_node': '/host/mysql_log/'})


@login_required
@csrf_exempt
def nav_manage(request):
    if request.method=='POST':
        action=request.POST.get('action')
        ip=request.POST.get('IP')
        domain=request.POST.get('domain')
        t=request.POST.get('type')
        site=request.POST.get('site')
        navigate=request.POST.get('navigate','')
        statics=request.POST.get('statics','')
        message=''

        if action=='add':
            message=utils.nav_add(ip,domain,t,site,navigate,statics)
        elif action=='mod':
            message=utils.nav_mod(ip,domain,t,navigate,statics)
        elif action=='del':
            message=utils.nav_del(ip,domain)
        return HttpResponse(message)
    else:
        return render(request, 'nav_manage.html')

@login_required
def nav_select(request):
    type=request.POST.get('t')
    type_list=models.NavType.objects.filter(type=type)
    list=[]
    for t in type_list:
        list.append([t.id,t.name])
    list=json.dumps(list)
    return HttpResponse(list)


@login_required
def mysql_user_manage(request):
    if request.method=='POST':
        ip=request.POST.get('IP')
        mysql_port=request.COOKIES['PORT']
        ssh_port=int(models.Hosts.objects.get(ip_addr=ip).port)
        user_list=utils.get_mysql_user(ip,ssh_port,mysql_port)
        user_list=json.dumps(user_list)
        return HttpResponse(user_list)
    else:
        host_list=models.Hosts.objects.all().values('ip_addr')
        return render(request,'mysql_user_manage.html',{'host_list':host_list,'active_node':'/mysql/user/'})

@login_required
def mysql_select_port(request):
    ip=request.POST.get('ip')
    ssh_port = models.Hosts.objects.get(ip_addr=ip).port
    port_list=utils.mysql_select_port(ip,ssh_port)
    port_list=json.dumps(port_list)
    return HttpResponse(port_list)


@login_required
def mysql_delete_user(request):
    ip=request.POST.get('ip')
    ssh_port = models.Hosts.objects.get(ip_addr=ip).port
    mysql_port=request.COOKIES['PORT']
    user=request.POST.get('user')
    host=request.POST.get('host')
    result=utils.del_mysql_user(ip,ssh_port,user,host,mysql_port)
    return HttpResponse(result)

@login_required
def mysql_modified_user(request):
    ip=request.POST.get('ip')
    mysql_port=request.COOKIES['PORT']
    ssh_port = models.Hosts.objects.get(ip_addr=ip).port
    user=request.POST.get('user')
    host=request.POST.get('host')
    password=request.POST.get('password')
    result=utils.mod_mysql_user(ip,ssh_port,user,host,password,mysql_port)
    return HttpResponse(result)

@login_required
def mysql_add_user(request):
    ip=request.POST.get('ip')
    mysql_port=request.COOKIES['PORT']
    ssh_port = models.Hosts.objects.get(ip_addr=ip).port
    user=request.POST.get('user')
    host=request.POST.get('host')
    dbname=request.POST.get('dbname')
    is_first=request.POST.get('is_first')
    password = request.POST.get('password','')
    db_type=request.POST.get('db_type')
    result=utils.add_mysql_user(ip,ssh_port,user,host,password,dbname,is_first,db_type,mysql_port)
    return HttpResponse(result)

@login_required
def mysql_add_database(request):
    ip=request.POST.get('ip')
    mysql_port = request.COOKIES['PORT']
    ssh_port = models.Hosts.objects.get(ip_addr=ip).port
    database=request.POST.get('database')
    result=utils.add_mysql_database(ip,ssh_port,mysql_port,database)
    return HttpResponse(result)

@login_required
def mysql_select_database(request):
    ip=request.POST.get('ip')
    mysql_port=request.COOKIES['PORT']
    ssh_port = models.Hosts.objects.get(ip_addr=ip).port
    result=utils.select_mysql_database(ip,ssh_port,mysql_port)
    db_list=json.dumps(result)
    return HttpResponse(db_list)


@login_required
def mysql_user_priv(request):
    ip=request.POST.get('ip')
    mysql_port=request.COOKIES['PORT']
    ssh_port = models.Hosts.objects.get(ip_addr=ip).port
    user=request.POST.get('user')
    host=request.POST.get('host')
    user_list=utils.mysql_user_priv(ip,ssh_port,user,host,mysql_port)
    user_list=json.dumps(user_list)
    return HttpResponse(user_list)

@login_required
def mysql_del_priv(request):
    ip=request.POST.get('ip')
    mysql_port=request.COOKIES['PORT']
    ssh_port=models.Hosts.objects.get(ip_addr=ip).port
    user=request.POST.get('user')
    host=request.POST.get('host')
    dbname=request.POST.get('dbname')
    print(ip,ssh_port,user,host,dbname)
    result=utils.mysql_del_priv(ip,ssh_port,user,host,dbname,mysql_port)
    return HttpResponse(result)

@login_required
def iso_upload(request):
    if request.method=='GET':
        group_list1 = models.UserProfile.objects.get(name=request.user.name).host_groups.select_related()
        host_list=[]
        for group in group_list1:
            host_list1=group.bind_hosts.select_related()
            host_list += [host.host.ip_addr for host in host_list1]
        host_list2 = models.UserProfile.objects.get(name=request.user.name).bind_hosts.select_related()
        host_list += [host.host.ip_addr for host in host_list2]
        host_list = list(set(host_list))
        return render(request,'iso_upload.html',{'active_node':'/iso/upload/','host_list':host_list})


@login_required
def iso_history(request):
    if request.method=='GET':
        return render(request,'iso_history.html',{'active_node':'/iso/history/'})
    else:
        action=request.POST.get('action')
        if action=='insert':
            date_time=request.POST.get('date_time')
            #date_time转成时间戳
            date_time=int(time.mktime(time.strptime(date_time,'%Y-%m-%d %H:%M:%S')))
            ip=request.POST.get('ip')
            filename=request.POST.get('filename')
            directory=request.POST.get('directory')
            status=request.POST.get('status')
            try:
                hisobj=models.UploadHistory(date_time=date_time,ipaddr=ip,filename=filename,directory=directory,status=status)
                hisobj.save()
                return HttpResponse('ok')
            except Exception as e:
                return HttpResponse('error')
        elif action=='select':
            from_time=request.POST.get('from_time',None)
            if from_time.strip()!='':
                from_time=int(time.mktime(time.strptime(from_time,'%Y-%m-%d')))
            else:
                from_time=0
            to_time=request.POST.get('to_time',None)
            if to_time.strip()!='':
                to_time = int(time.mktime(time.strptime(to_time, '%Y-%m-%d'))) + 60*60*24 - 1
            else:
                to_time=0
            try:
                page=int(request.POST.get('page',None))
            except Exception as e:
                page=1
            if from_time == 0 and to_time == 0:
                his_list = models.UploadHistory.objects.all()
            elif from_time and to_time == 0:
                his_list = models.UploadHistory.objects.filter(date_time__gte=from_time)
            elif from_time == 0 and to_time:
                his_list = models.UploadHistory.objects.filter(date_time__lte=to_time)
            else:
                his_list = models.UploadHistory.objects.filter(date_time__lte=to_time).filter(date_time__gte=from_time)
            page_num = int(len(his_list) / 10) + 1
            his_list = [[h.id, h.date_time, h.ipaddr, h.filename, h.directory, h.status] for h in his_list][(page - 1) * 10:page * 10]
            result = {'his_list': his_list, 'page_num': page_num}

            result = json.dumps(result)
            return HttpResponse(result)




@login_required
def dns_info(request):
    if request.method=='GET':
        return render(request,'dns_info.html',{'active_node':'/dns/info/'})
    else:
        domain=request.POST.get('key',None)
        page=int(request.POST.get('page',None))
        if domain:
            dns_list=models.DNSDomain.objects.filter(domain=domain)
        else:
            try:
                page=page
            except Exception as e:
                page=1
            dns_list = models.DNSDomain.objects.all()
        page_num = int(len(dns_list) / 10) + 1
        dns_list=[[h.domain,h.account.username,h.account.product] for h in dns_list][(page-1)*10:page*10]
        result={'dns_list':dns_list,'page_num':page_num}

        result=json.dumps(result)
        return HttpResponse(result)


@login_required
def log_download(request):
    if request.method=="GET":
        host_id=request.GET.get('host_id')
        log_path=request.GET.get('log_path')
        hostobj = models.Hosts.objects.get(id=host_id)
        result=utils.sftp_down(hostobj.ip_addr,hostobj.port,log_path)

    return HttpResponse(result)

@login_required
def user_audit(request,user_id):

    user_obj = models.UserProfile.objects.get(id=int(user_id))
    department_list = models.Department.objects.all()
    user_login_records = models.AuditLog.objects.filter(user_id=user_obj.id,action_type=1).order_by('-date')
    user_multi_task_records = models.TaskLog.objects.filter(user_id= user_obj.id).order_by('-start_time')
    paginator = Paginator(user_login_records,10)
    paginator_multi = Paginator(user_multi_task_records,10)
    page = request.GET.get('page')
    data_type = request.GET.get('type')

    try:
        login_records = paginator.page(page)
    except PageNotAnInteger:
        login_records = paginator.page(1)
    except EmptyPage:
        login_records = paginator.page(paginator.num_pages)

    try:
        multitask_records = paginator_multi.page(page)
    except PageNotAnInteger:
        multitask_records = paginator_multi.page(1)
    except EmptyPage:
        multitask_records = paginator_multi.page(paginator_multi.num_pages)


    return  render(request,'user_audit.html',{
        'department_list':department_list,
        'user_obj':user_obj,
        'user_login_records':login_records,
        'multitask_records':multitask_records,
        'active_node':'/user_audit/1/',
        'data_type': data_type #for tab switch usage
    })


@login_required
def audit(request):

    audit_log_list = tables.table_filter(request,admin.AuditLogAdmin,models.AuditLog)
    order_res = tables.get_orderby(request, audit_log_list, admin.AuditLogAdmin)
    paginator = Paginator(order_res[0], admin.AuditLogAdmin.list_per_page)

    page = request.GET.get('page')
    try:
        audit_log_objs = paginator.page(page)
    except PageNotAnInteger:
        audit_log_objs = paginator.page(1)
    except EmptyPage:
        audit_log_objs = paginator.page(paginator.num_pages)

    table_obj = tables.TableHandler(request,
                                    models.AuditLog,
                                    admin.AuditLogAdmin,
                                    audit_log_objs,
                                    order_res)

    return render(request,"audit.html",{'table_obj':table_obj,
                                        'paginator':paginator})



@csrf_exempt
def update_https(request):
    message={}
    if request.method=='POST':
        timestamp=request.POST.get('timestamp')
        domain_name=request.POST.get('domain_name')
        certificate=request.POST.get('certificate')
        privatekey=request.POST.get('privatekey')
        token=request.POST.get('token')
        ip=request.POST.get('ip')
        current_time=int(time.time())
        if current_time-int(timestamp) > 30:
            message['message']='The timestamp is out.'
            message['status']='error'
            return HttpResponse(json.dumps(message))
        m=hashlib.md5()
        m.update((str(timestamp)+'miguan').encode('utf-8'))
        passwd=m.hexdigest()
        if passwd!=token:
            message['message']='The auth is invalid.'
            message['status']='error'
            return HttpResponse(json.dumps(message))
        try:
            cdnobj=models.CDNDomain.objects.get(domain_name=domain_name,ishttps=1,source__contains=ip)
        except Exception as e:
            message['message']='%s 域名没有添加CDN或者没有开启https或者源IP不匹配！' % domain_name
            message['status']='error'
            print(message)
            return HttpResponse(json.dumps(message))

        message=aliyun.update_https(cdnobj,certificate,privatekey)
        return HttpResponse(message)


@login_required
def multitask_task_action(request):
    if request.method == 'POST':
        action = request.POST.get('action')
        m = host_mgr.MultiTask(action,request)
        res = m.run()

        return  HttpResponse(json.dumps(res))

@login_required
def multi_task_log_detail(request,task_id):

    log_obj = models.TaskLog.objects.get(id=task_id)

    return render(request,'multi_task_log_detail.html',{'log_obj':log_obj})

@login_required
def audit_cmd_logs(request):
    session_id  = request.GET.get('session_id')
    if session_id:
        session_id = int(session_id)
        cmd_records = list(models.AuditLog.objects.filter(session_id = session_id,action_type=0).values().order_by('date'))
        if len(cmd_records)==0:
            auditobj = models.AuditLog.objects.get(session_id=session_id, action_type=1)
            sessionobj=auditobj.session
            returncode=subprocess.call("ps ajfx|grep ssh|grep -v sshpass|grep -v grep|grep %s" % (sessionobj.tag),shell=True)
            if returncode:
                session_log_file="%s/logs/audit/%s/session_%s.log" %(settings.BASE_DIR,sessionobj.date.strftime("%Y_%m_%d"),session_id)
                log_parser = session_audit.AuditLogHandler(session_log_file)
                log_data = log_parser.parse()
                sessionobj.cmd_count=len(log_data)
                sessionobj.closed=True
                sessionobj.save()

                for log in log_data:
                    row = models.AuditLog(
                        session=sessionobj,
                        user=auditobj.user,
                        host=auditobj.host,
                        action_type=log[2],
                        cmd=log[1],
                        date=log[0]
                    )
                    row.save()
                cmd_records = list(models.AuditLog.objects.filter(session_id=session_id, action_type=0).values().order_by('date'))
        data = {
            'data':cmd_records,
            'action_choices':models.AuditLog.action_choices
        }

        return  HttpResponse(json.dumps(data,default=json_date_handler))

@login_required
def user_login_counts(request):
    filter_time_stamp = request.GET.get('time_stamp')
    assert  filter_time_stamp.isdigit()
    filter_time_stamp = int(filter_time_stamp) / 1000
    filter_date_begin = datetime.datetime.fromtimestamp(filter_time_stamp)
    filter_date_end = filter_date_begin + datetime.timedelta(days=1)

    user_login_records = models.Session.objects.filter(date__range=[filter_date_begin,filter_date_end]).\
        values('bind_host',
               'bind_host__host_user__username',
               'user',
               'user__name',
               'bind_host__host__hostname',
               'date')

    return  HttpResponse(json.dumps(list(user_login_records),default=json_date_handler))



@login_required
def session_reccord(request,session_id):
    try:

        session_obj = models.Session.objects.get(id=session_id)
        print("session obj:",session_obj)
        session_log_file = "%s/%s/session_%s.log" %(settings.SESSION_AUDIT_LOG_DIR,
                                                    session_obj.date.strftime( "%Y_%m_%d"),
                                                    session_obj.id)

        if os.path.isfile(session_log_file):
            log_wash = session_audit.AuditLogHandler(session_log_file)
            log_data = log_wash.parse()
            #update session stay time and cmd count
            session_obj.cmd_count = len(log_data)
            if len(log_data ) >1:
                last_cmd_time = log_data[-1][0]
                last_cmd_datetime_str = "%s %s"%(session_obj.date.strftime( "%Y_%m_%d"), last_cmd_time)
                #print("last_cmd_datetime_str",last_cmd_datetime_str)
                last_cmd_struct_time = time.strptime(last_cmd_datetime_str,"%Y_%m_%d %H:%M:%S")
                last_cmd_timestamp = time.mktime(last_cmd_struct_time)
                #print('last cmd timestamp:',last_cmd_timestamp)
                session_obj.stay_time = last_cmd_timestamp - session_obj.date.timestamp()
                session_obj.save()
        else:
            log_data = [['n/a','---no session log---']]
        # if os.path.isfile(session_log_file):
        #     session_log = open(session_log_file).read()
        # else:
        #     print('file not exist ',session_log_file)
        #     session_log = '---no session log---'

        return render(request,"session_log.html",{'session_data':log_data,'session_obj':session_obj})
    except ObjectDoesNotExist as e:
        return HttpResponse(e)


@permissions.check_permission
@login_required
def configure_url_dispatch(request,url):
    print('---url dispatch',url)
    #print(enabled_admins)
    if url in enabled_admins:
        #print(enabled_admins[url])

        if request.method == "POST":
            print('post-->', request.POST)

            delete_tag = request.POST.get("_delete_confirm")
            if delete_tag == "yes":
                del_ids = request.POST.getlist("deleted_objs")

                enabled_admins[url].model.objects.filter(id__in=del_ids).delete()

            else:

                admin_action = request.POST.get('admin_action')

                admin_obj = enabled_admins[url]
                if hasattr(admin_obj, admin_action):
                    admin_action_func = getattr(admin_obj, admin_action)
                    return admin_action_func(request)
                else:
                    raise NotImplementedError("admin_action %s cannot find" % admin_action)


        querysets = tables.table_filter(request, enabled_admins[url],
                                        enabled_admins[url].model)
        searched_querysets = tables.search_by(request,querysets,enabled_admins[url])
        order_res = tables.get_orderby(request, searched_querysets, enabled_admins[url])

        paginator = Paginator(order_res[0], enabled_admins[url].list_per_page)

        page = request.GET.get('page')
        try:
            table_obj_list = paginator.page(page)
        except PageNotAnInteger:
            table_obj_list = paginator.page(1)
        except EmptyPage:
            table_obj_list = paginator.page(paginator.num_pages)

        table_obj = tables.TableHandler(request,
                                        enabled_admins[url].model,
                                        enabled_admins[url],
                                        table_obj_list,
                                        order_res)



        return render(request,'king_admin/model_obj_list.html',
                                                {'table_obj':table_obj,
                                                 'active_node': '/configure/index/',
                                                 'paginator':paginator})

    else:
        raise Http404("url %s not found" % url )

@permissions.check_permission
@login_required
def table_change(request,table_name,obj_id):
    print("table change:",table_name ,obj_id)
    if table_name in enabled_admins:
        #print(enabled_admins[table_name])
        obj = enabled_admins[table_name].model.objects.get(id=obj_id)
        #print("obj....change",obj)
        fields = []
        for field_obj in enabled_admins[table_name].model._meta.fields:
            if field_obj.editable :
                fields.append(field_obj.name)

        for field_obj in enabled_admins[table_name].model._meta.many_to_many:
            fields.append(field_obj.name)
        #print('fields', fields)
        model_form = forms.create_form(enabled_admins[table_name].model, fields,enabled_admins[table_name],request=request)

        if request.method == "GET":
            form_obj = model_form(instance=obj)

        elif request.method == "POST":
            print("post:",request.POST)
            form_obj = model_form(request.POST,instance=obj)
            if form_obj.is_valid():
                form_obj.save()

        return render(request,'king_admin/table_change.html',
                      {'form_obj':form_obj,
                      'active_node': '/configure/index/',
                      'model_name':enabled_admins[table_name].model._meta.verbose_name,
                      'model_db_table': enabled_admins[table_name].model._meta.db_table,
                       'admin_class':enabled_admins[table_name]

                        })
    else:
        raise Http404("url %s not found" % table_name )


@permissions.check_permission
@login_required
def configure_index(request):

    return render(request,'king_admin/index.html', {'enabled_admins':enabled_admins})

def table_add(request,table_name):
    print("request path:",request.path)
    if table_name in enabled_admins:
        fields = []
        for field_obj in enabled_admins[table_name].model._meta.fields:
            if field_obj.editable:
                fields.append(field_obj.name)
        for field_obj in enabled_admins[table_name].model._meta.many_to_many:
            fields.append(field_obj.name)
        if enabled_admins[table_name].add_form == None:
            model_form = forms.create_form(enabled_admins[table_name].model, fields,enabled_admins[table_name],form_create=True,request=request)
        else: #this admin has customized  creation form defined
            model_form = enabled_admins[table_name].add_form

        if request.method == "GET":
            form_obj = model_form()
        elif request.method == "POST":
            print(request.POST)
            form_obj = model_form(request.POST)
            if form_obj.is_valid():
                form_obj.save()
                print("form obj:",form_obj.cleaned_data,form_obj.instance.id)
                redirect_url = '/%s/change/%s' %(request.path.strip("/add"), form_obj.instance.id)
                if request.POST.get('_continue') is  None:#save and add another button
                    return redirect(redirect_url)

                #print("----continue....",request.POST)
                form_obj = model_form()

        return render(request, 'king_admin/table_add.html',
                      {'form_obj': form_obj,
                       'active_node': '/configure/index/',
                       'model_name': enabled_admins[table_name].model._meta.verbose_name,
                       'model_db_table':enabled_admins[table_name].model._meta.db_table,
                       'admin_class': enabled_admins[table_name]
                       })

    else:
        raise Http404("url %s not found" % table_name)


def table_del(request,table_name,obj_id):

    if table_name in enabled_admins:
        obj = enabled_admins[table_name].model.objects.get(id=obj_id)

        return render(request,'king_admin/table_delete.html',{
            'model_name': enabled_admins[table_name].model._meta.verbose_name,
            'model_table_name':enabled_admins[table_name].model._meta.model_name,
            'model_db_table':enabled_admins[table_name].model._meta.db_table,
            'obj':obj,
            'app_label':obj._meta.app_label
                                })